/*------------------------------------------------------------------------------

Publication: Schakel, W. (2020) Representing the Rich: Economic and Political
	Inequality in Established Democracies. University of Amsterdam: Doctoral
	Dissertation.

Chapter: 2

Description: This do-file contains the commands for the figures presented in the
	text and online appendices of chapter 2.

Sections:

	1. Figure 2.1 (mean spending preferences of low and high incomes)
	2. Figure 2.2 (average change in welfare state generosity)
	3. Figure 2.3 (predicted values of changes in welfare state generosity)
	4. Figure 2D.1 (leverage-versus-residual-squared plot)

Data: Schakel_2020_Chapter2_1_Data (see Schakel_2020_Chapter2_2_Preparation
	do-file for original sources)

------------------------------------------------------------------------------*/

global data "YOUR PATH HERE"

use "$data\Schakel_2020_Chapter2_1_Data.dta", clear

/*------------------------------------------------------------------------------
 1. Figure 2.1 (mean spending preferences of low and high incomes)
------------------------------------------------------------------------------*/

tabstat p10 p50 p90, by(topic) s(mean semean) c(stat) nototal save
matrix define y2_=r(Stat1)'\r(Stat2)'\r(Stat3)'
svmat y2_

gen lowbar2 = y2_1 - y2_2
gen highbar2 = y2_1 + y2_2

* This matrix contains the values of the x-axis and the income groups
matrix define x2_=(1,2,3,5,6,7,9,10,11\1,2,3,1,2,3,1,2,3)'
svmat x2_

twoway (bar y2_1 x2_1 if x2_2==1, fc(gs7) fi(100) lc(gs7))					///
	(bar y2_1 x2_1 if x2_2==2, fc(gs10) fi(100) lc(gs10))					///
	(bar y2_1 x2_1 if x2_2==3, fc(gs13) fi(100) lc(gs13))					///
	(rcap lowbar2 highbar2 x2_1, lc(black) msize(zero)),					///
	xt("") yt("Average spending preferences", size(medium))					///
	xlab(2 "Health care" 6 "Pensions" 10 "Unemployment", labs(medium)		///
	labgap(tiny) notick) legend(order(1 "10" 2 "50" 3 "90")					///
	bm(medium) symx(8) c(1) pos(3) size(medium) reg(lc(white))				///
	title("Percentile", size(medium) bex c(black) j(left)))					///
	plotr(m(4 4 0 0) lc(white)) graphr(m(2 0 1 3) fc(white) lc(white))		///
	ylab(-20(20)60, ang(hor) nogrid) yline(0, lc(gs0) lw(thin)) xsiz(6.7) ysiz(4)

drop y2_1-x2_2

/*------------------------------------------------------------------------------
 2. Figure 2.2 (average change in welfare state generosity)
------------------------------------------------------------------------------*/

* The commands before the graph are necessary because this is the only way we
* know of to put the country labels in the right place
encode country2 if dgentav14!=. & pct!=., gen(ctemp)
gen dgen_country=.

quietly forval i=1/20 {
	sum dgentav14 if ctemp==`i'
	replace	dgen_country=r(mean) if ctemp==`i'
	}

sort dgen_country
egen pickone=tag(country)

egen x2=rank(dgen_country) if pickone==1, unique

gen y=0

twoway (dot y x2 if dgen_country>0, hor mc(gs0%0) msiz(vtiny)				///
	mlabel(country) mlabp(9) mlabs(small) mlabc(gs0) ndots(0))				///
	(dot y x2 if dgen_country<0, hor mc(gs0%0) msiz(vtiny)					///
	mlabel(country) mlabp(3) mlabs(small) mlabc(gs0) ndots(0))				///
	(bar dgen_country x2, hor ysc(off) barw(.7) fi(100) col(gs6)			///
	xlab(-6(2)6, labs(small)) ylab(1(20)20, nogrid)							///
	xtitle("Four-year change in welfare generosity (%)", s(small)			///
	m(0 0 0 2)) xline(0, lc(gs0) lw(vthin)) legend(off) xsize(6) ysize(5)	///
	graphr(m(medsmall) fc(white)) plotr(m(0 0 2 1) lc(white)))

drop ctemp-y

/*------------------------------------------------------------------------------
 3. Figure 2.3 (predicted values of changes in welfare state generosity)
------------------------------------------------------------------------------*/

* 10th percentile
qui mixed dgentav14 p10 p90 gent loggdpt growtht unempt i.topic i.wave || country:, vce(rob)
qui margins, at(p10=(-24(2)74)) atmeans
marginsplot, plotop(recast(line) lc(gs0))									///
	ciopts(lp(dash) recast(rline) lc(gs0))									///
	xt("Spending preferences", m(0 0 0 2)) yt("Change in generosity (%)",	///
	m(zero)) xlab(-25(25)75) ylab(-8(4)8, angle(hor) nogrid)				///
	text(7.5 25 "10th percentile", s(medlarge))								///
	title(, si(zero) m(zero) c(white)) plotr(m(2 2 3 2) lc(white))			///
	graphr(m(2 0 1 2) fc(white) lc(white)) name(a, replace)

* 50th percentile
qui mixed dgentav14 p50 gent loggdpt growtht unempt i.topic i.wave || country:, vce(rob)
qui margins, at(p50=(-24(2)74)) atmeans
marginsplot, plotop(recast(line) lc(gs0))									///
	ciopts(lp(dash) recast(rline) lc(gs0))									///
	xt("Spending preferences", m(0 0 0 2)) yt("", m(zero))					///
	xlab(-25(25)75) ylab(-8(4)8, angle(hor) nogrid)							///
	text(7.5 25 "50th percentile", s(medlarge))								///
	title(, si(zero) m(zero) c(white)) plotr(m(2 2 3 3) lc(white))			///
	graphr(m(4 1.5 1 2) fc(white) lc(white)) name(b, replace)

* 90th percentile
qui mixed dgentav14 p10 p90 gent loggdpt growtht unempt i.topic i.wave || country:, vce(rob)
qui margins, at(p90=(-24(2)74)) atmeans
marginsplot, plotop(recast(line) lc(gs0))									///
	ciopts(lp(dash) recast(rline) lc(gs0))									///
	xt("Spending preferences", m(0 0 0 2)) yt("", m(zero))					///
	xlab(-25(25)75) ylab(-8(4)8, angle(hor) nogrid)							///
	text(7.5 25 "90th percentile", s(medlarge))								///
	title(, si(zero) m(zero) c(white)) plotr(m(2 2 3 1.5) lc(white))		///
	graphr(m(2.5 3 1 2) fc(white) lc(white)) name(c, replace)

* 10th, 50th and 90th percentile combined
graph combine a b c, graphr(m(zero)) xsize(14) ysize(5) col(3) scale(1.75)

/*------------------------------------------------------------------------------
 4. Figure 2D.1 (leverage-versus-residual-squared plot)
------------------------------------------------------------------------------*/

reg dgentav14 p10 p90 gent loggdpt growtht unempt i.topic i.wave
lvr2plot, msiz(small) mc(gs0) ylab(, ang(hor) nogrid)						///
	xtitle(, m(0 0 0 2)) ytitle(, m(0 1 0 0)) 								///
	plotr(lc(white)) graphr(m(2 4 2 2) fc(white) lc(white))					///
	text(.068 .266 "Unemployment, Sweden 2006")
